package drds.binlog.store;

import drds.binlog.common.Authors;
import drds.binlog.common.position.LogIdentity;
import drds.binlog.data_object.Entry;
import drds.binlog.data_object.EntryType;
import drds.binlog.data_object.EventType;
import drds.binlog.data_object.KeyValuePair;
import drds.common.Author;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * store存储数据对象
 */
@Author(name = Authors.LI_YANG)
public class Event
{

    @Setter
    @Getter
    private LogIdentity logIdentity;                            // 记录数据产生的来源

    @Setter
    @Getter
    private long executeDateTimeLong;
    @Setter
    @Getter
    private EntryType entryType;
    @Setter
    @Getter
    private String journalName;
    @Setter
    @Getter
    private long position;
    @Setter
    @Getter
    private long serverId;
    @Setter
    @Getter
    private EventType eventType;
    @Setter
    @Getter
    private String gtid;
    @Setter
    @Getter
    private long rawLength;
    @Setter
    @Getter
    private int rowsCount;

    public Event()
    {
    }

    public Event(LogIdentity logIdentity, Entry entry)
    {
        this.logIdentity = logIdentity;
        this.entryType = entry.getEntryType();
        this.executeDateTimeLong = entry.getHeader().getExecuteDateTimeLong();
        this.journalName = entry.getHeader().getLogFileName();
        this.position = entry.getHeader().getLogFileOffset();
        this.serverId = entry.getHeader().getServerId();
        this.gtid = entry.getHeader().getGtid();
        this.eventType = entry.getHeader().getEventType();
        // build raw
        //this.rawEntry = entry.toByteString();
        this.rawLength = 0;// rawEntry.size();
        if (entryType == EntryType.rowdata)
        {
            List<KeyValuePair> propertyList = entry.getHeader().getPropertyList();
            if (propertyList != null)
            {
                for (KeyValuePair p : propertyList)
                {
                    if ("rowsCount".equals(p.getKey()))
                    {
                        rowsCount = Integer.parseInt(p.getValue());
                        break;
                    }
                }
            }
        }
    }


}
